'''
text 2 image
dashscope
'''

from http import HTTPStatus
from urllib.parse import urlparse, unquote
from pathlib import PurePosixPath
import requests
from dashscope import ImageSynthesis #
import os

# prompt = "一间有着精致窗户的花店，漂亮的木质门，摆放着花朵"
prompt = ("一对新人站在金色沙滩上，背景是夕阳西下的海景。新娘穿着白色拖地婚纱，头纱随风飘扬，手中捧着一束粉色玫瑰花。"
          "新郎身着深蓝色西装，两人深情对视，脸上洋溢着幸福的笑容。海浪轻轻拍打着岸边，远处有几只飞翔的海鸥，整个场景充满浪漫和温馨的气息。")

prompt = (r'在一座古色古香的中国古典园林里，一对新人正在拍摄婚纱照。新娘身着红色龙凤褂，头戴金饰，手执一把折扇，显得端庄典雅。新郎则穿着黑色中式礼服，手持一把玉如意。'
          r'他们站在一座古老的亭子前，周围是盛开的梅花和翠绿的竹林，阳光透过树叶洒下斑驳的光影，营造出一种宁静而优美的氛围。')

print('----sync call, please wait a moment----')
rsp = ImageSynthesis.call(api_key=os.getenv("DASHSCOPE_API_KEY"),
                          model="wanx2.1-t2i-turbo",
                          prompt=prompt,
                          n=1,
                          size='1024*1024')
print('response: %s' % rsp)
if rsp.status_code == HTTPStatus.OK:
    # 在当前目录下保存图片
    for result in rsp.output.results:
        file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
        with open('./%s' % file_name, 'wb+') as f:
            f.write(requests.get(result.url).content)
else:
    print('sync_call Failed, status_code: %s, code: %s, message: %s' %
          (rsp.status_code, rsp.code, rsp.message))